package no21.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 测试阻塞队列
 * @author gao
 *
 */
public class LiftOffRunner implements Runnable {

	private BlockingQueue<Liftoff> queue;
	public LiftOffRunner(BlockingQueue<Liftoff> queue) {
		this.queue = queue;
	}
	@Override
	public void run() {
		getLiftoff();
	}
	private Liftoff getLiftoff() {
		try {
			//获取对象时阻塞
			System.out.println("taking.....................");
			Liftoff l = queue.take();
			//真正获得对象
			System.out.println("taking over................");
			return l;
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		ExecutorService exec = Executors.newCachedThreadPool();
		BlockingQueue<Liftoff> queuetemp = new LinkedBlockingQueue<Liftoff>();
		exec.execute(new LiftOffRunner(queuetemp));
		try {
			TimeUnit.MILLISECONDS.sleep(100);
			
			System.out.println("test");
			//向阻塞队列中增加元素
			//等待的线程可以直接获得
			queuetemp.put(new Liftoff());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		exec.shutdown();
	}
}
